package com.zmy.xcx.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmy.xcx.common.convention.exception.ClientException;
import com.zmy.xcx.dao.entity.Medication;
import com.zmy.xcx.dao.entity.MedicineTreatment;
import com.zmy.xcx.dao.entity.TreatmentMethod;
import com.zmy.xcx.dao.mapper.MedicineTreatmentMapper;
import com.zmy.xcx.service.MedicationService;
import com.zmy.xcx.service.MedicineTreatmentService;
import com.zmy.xcx.service.TreatmentMethodService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 药品-治疗方法关联服务实现类
 */
@Slf4j
@Service
public class MedicineTreatmentServiceImpl extends ServiceImpl<MedicineTreatmentMapper, MedicineTreatment> 
        implements MedicineTreatmentService {

    @Autowired
    private MedicationService medicationService;

    @Autowired
    private TreatmentMethodService treatmentMethodService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addTreatmentForMedicine(Long medicineId, Long methodId) {
        log.info("开始添加药品治疗方法关联，medicineId: {}, methodId: {}", medicineId, methodId);
        try {
            // 验证药品和治疗方法是否存在
            medicationService.getMedication(medicineId);
            treatmentMethodService.getTreatmentMethod(methodId.toString());

            // 创建关联关系
            MedicineTreatment medicineTreatment = MedicineTreatment.builder()
                    .medicineId(medicineId)
                    .methodId(methodId)
                    .build();

            save(medicineTreatment);
            log.info("添加药品治疗方法关联成功");
        } catch (Exception e) {
            log.error("添加药品治疗方法关联失败", e);
            throw new ClientException("添加药品治疗方法关联失败");
        }
    }

    @Override
    public List<TreatmentMethod> getTreatmentMethodsByMedicineId(Long medicineId) {
        log.info("开始查询药品相关的治疗方法，medicineId: {}", medicineId);
        try {
            List<Long> methodIds = baseMapper.findMethodIdsByMedicineId(medicineId);
            List<TreatmentMethod> methods = new ArrayList<>();
            
            for (Long methodId : methodIds) {
                TreatmentMethod method = treatmentMethodService.getTreatmentMethod(methodId.toString());
                if (method != null) {
                    methods.add(method);
                }
            }
            
            log.info("查询药品相关的治疗方法成功，找到{}个方法", methods.size());
            return methods;
        } catch (Exception e) {
            log.error("查询药品相关的治疗方法失败，medicineId: {}", medicineId, e);
            throw new ClientException("查询药品相关的治疗方法失败");
        }
    }

    @Override
    public List<Medication> getMedicationsByMethodId(Long methodId) {
        log.info("开始查询治疗方法相关的药品，methodId: {}", methodId);
        try {
            List<Long> medicineIds = baseMapper.findMedicineIdsByMethodId(methodId);
            List<Medication> medications = new ArrayList<>();
            
            for (Long medicineId : medicineIds) {
                Medication medication = medicationService.getMedication(medicineId);
                if (medication != null) {
                    medications.add(medication);
                }
            }
            
            log.info("查询治疗方法相关的药品成功，找到{}个药品", medications.size());
            return medications;
        } catch (Exception e) {
            log.error("查询治疗方法相关的药品失败，methodId: {}", methodId, e);
            throw new ClientException("查询治疗方法相关的药品失败");
        }
    }
} 